package com.qzsoft.zhjd.server.modules.ludong.contaminant.service.impl;

import com.qzsoft.zhjd.server.modules.ludong.contaminant.entity.ContaminantMonitorDetailEntity;
import com.qzsoft.zhjd.server.modules.ludong.contaminant.entity.ContaminantMonitorEntity;
import com.qzsoft.zhjd.server.modules.ludong.contaminant.mapper.ContaminantMonitorMapper;
import com.qzsoft.zhjd.server.modules.ludong.contaminant.service.ContaminantMonitorDetailService;
import com.qzsoft.zhjd.server.modules.ludong.contaminant.service.ContaminantMonitorService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service("contaminantMonitorService")
public class ContaminantMonitorServiceImpl extends ServiceImpl<ContaminantMonitorMapper, ContaminantMonitorEntity> implements ContaminantMonitorService {

    @Autowired
    private ContaminantMonitorDetailService contaminantMonitorDetailService;


    /**
     * 功能描述: 插入环境监控数据
     * @param entity 环境监控数据
     * @param list 环境监控详细数据
     * @return : void
     * @author : liugf
     * @date : 2020/6/10 20:09
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void insertMonitorAndDetailData(ContaminantMonitorEntity entity, List<ContaminantMonitorDetailEntity> list) {
        if (StringUtils.isNotBlank(entity.getId())){
            this.removeById(entity.getId());
            this.save(entity);
            for(ContaminantMonitorDetailEntity contaminantMonitorDetailEntity : list){
                if (StringUtils.isNotBlank(contaminantMonitorDetailEntity.getDataId())) {
                    contaminantMonitorDetailService.removeById(contaminantMonitorDetailEntity.getDataId());
                }
            }
            contaminantMonitorDetailService.saveBatch(list);
        }
    }

}
